//
//  GuidePageViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/9/18.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class GuidePageViewController: BaseViewController {
    
    let pageControl = CustomPageControl.init(frame: CGRect.init(x: 0, y: UIScreen.ScreenHeight - 30 - (Utils.isIphoneX() ? 34 : 0), width: UIScreen.ScreenWidth, height: 10))
    
    var scrollViwe:UIScrollView!
    
    let imageArray = ["ydy1","ydy2","ydy3"]
    
    var btn:UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.scrollViwe = UIScrollView.init(frame: self.view.bounds)
        self.scrollViwe.isPagingEnabled = true
        self.scrollViwe.delegate = self
        self.view.addSubview(self.scrollViwe)
        
        for (idx,imageName) in self.imageArray.enumerated() {
            let imageV = UIImageView.init(frame: CGRect.init(x: CGFloat(idx) * UIScreen.ScreenWidth, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
            imageV.image = UIImage.init(named: imageName)
            self.scrollViwe.addSubview(imageV)
        }
        self.scrollViwe.contentSize = CGSize.init(width: UIScreen.ScreenWidth * CGFloat(self.imageArray.count), height: UIScreen.ScreenHeight)
        
        btn = UIButton.init(type: .custom)
        btn.frame = CGRect.init(x: (UIScreen.ScreenWidth - 176/2) * 0.5, y: UIScreen.ScreenHeight - (50 + (Utils.isIphoneX() ? 34 : 0)) - 44, width: 176/2, height: 88/2)
        btn.setBackgroundImage(UIImage.init(named: "button_2"), for: .normal)
        btn.setTitle("立即进入", for: .normal)
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        btn.isHidden = true
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 8, 0)
        self.view.addSubview(btn)
        
        self.pageControl.numberOfPages = self.imageArray.count
        self.pageControl.setValue(UIImage.init(named: "XZ"), forKey: "_currentPageImage")
        self.pageControl.setValue(UIImage.init(named: "wzx"), forKey: "_pageImage")
        self.view.addSubview(self.pageControl)
    }
    
    @objc func btnClick(){
        (UIApplication.shared.delegate as! AppDelegate).addClildCtrl()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension GuidePageViewController:UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index = Int(scrollViwe.contentOffset.x / (UIScreen.ScreenWidth))
        self.pageControl.currentPage = index
        if index == self.imageArray.count - 1 {
            btn.isHidden = false
        }else{
            btn.isHidden = true
        }
    }
}


















